var t = getApp();
var app=getApp()

Page({
    data: {
        checkID: "",
        vote_id: "",
        vote:{},
        canshow:true,
        default_img:'http://bmobfile.twho.top/2020/04/17/2a60bcae4049592f80a057b06e205801.png',
        hidden:!0,
        answer:'',
        answerList:[],
        content1:[],
        content2:[],
        voteIndex:1,
        loading:true
    },
         showImg(t){
        var e = t.currentTarget.dataset.url;
       
        if(!e){return}
        wx.previewImage({
            current: e,
            urls: [ e ]
        });
    },
    onLoad: function(t) {
        var e = this;
        //let vid="DlffW33B"
        let vid=t.tid
        console.log(t)
        if(!vid){return}
        e.setData({
            vote_id: vid
        })
        
        e.getVotes(vid);
        var uid=wx.getStorageSync('uid')
        if(uid){
            this.getAnswer(uid,vid)
        }
    },
    getAnswer(uid,vid){
        let that=this;
        if(!uid){
          uid=wx.getStorageSync('uid')
        }
        
        let param={
      
            vid:vid
        }
        let attr={
            limit:200,
            order:'-createdAt'
          
        } 
        let include='userinfo'
        wx.Als.bmobFunc("vote_answer", 'all',param,attr,include).then(res => {
        
            if(res&&res[0]){
           
              var content1=[],content2=[],content3=[];
              console.log(content1)
              res.forEach(item=>{
                if(item.content==1){
                  content1=content1.concat(item);
                }
                if(item.content==2){
                  content2=content2.concat(item)
                }
                if(item.uid==uid){
                    console.error(item)
                  content3=[item]
                }else{
                    console.log("buuu")
                }
              })
          
               
               that.setData({
                   answer:content3[0],
                   content1:content1,
                   content2:content2

               })
            }

        })
    },
    agree: function(t) {
        var e = t.currentTarget.dataset.id, a = this;
        let info=t.currentTarget.dataset.info;
        let type=t.currentTarget.dataset.type;
        let index=t.currentTarget.dataset.index;
        if(info){
            info=Number(info)
        }
     
        var uid=wx.getStorageSync('uid');
            console.log(info);
          
        if(!e||!uid||this.data.canshow==false){return}
        wx.showToast({
          title: '提交中...',
          icon:'none'
        })
        let param={
            uid:uid,
            vid:e
        }
        let attr={
            limit:1
        }
        this.setData({
            canshow:false
        })
        wx.Als.bmobFunc('vote_answer','all',param,attr).then(res=>{
           
            if(res&&res.length==0){
                a.add_vote(uid,e,info,index,type);
                app.addVoteNum(uid)
                var voteinfo=a.data.vote
                var content=type;
                voteinfo.group_count=voteinfo.group_count+1;
                var  contentinfo='vote_content'+content;
                voteinfo[contentinfo]= voteinfo[contentinfo]+1;
                a.setData({
                    vote:voteinfo,
                    answer:{content:content,uid:uid}
                })
               
            }else{
                wx.showToast({
                  title: '已经回答',
                  icon:'none'
                })
            }

        })
        setTimeout(function(res){
            a.setData({
              canshow:true
            })
          },1000);
    },
    update_list(aid,vid,content=1){
let query = wx.Bmob.Query('vote_list');
const pointer = wx.Bmob.Pointer('vote_answer')
const relID = pointer.set(aid)
content=Number(content)
// const relation = wx.Bmob.Relation('vote_answer') // 需要关联的表
// const relID = relation.add([aid,"vid"]) 
query.get(vid).then(res => {
  console.log(res)
  console.log("udpate")
  let c='vote_content'+content;
  let num1=Number(res[c])+1;
  console.log(num1);
  console.log("====")
  let num2=Number(res.group_count)+1;
 res.set('vote_content'+content,num1);
 res.set('group_count',num2);
 res.set('newid',relID); 
   res.save();

}).catch(err => {
  console.log(err)
})
    },
    add_vote(uid,vid,content1='',index=0,type=1){
        let a=this;
        let content=Number(content1)
     
        let param={
            uid:uid,
            vid:vid,
            content:content
        }
        wx.Als.bmobFunc('vote_answer', 'add', param).then(res => {
          
           
            let uid=wx.getStorageSync('uid')
            if(res){
                let answer={
                    uid:uid,
                    content:content,
                    vid:vid
                }
                a.setData({
                    answer:answer
                })
                a.update_list(res.objectId,vid,content);
            }
           
          })
    },

    add_list(title,content1,content2,vote_content1=0,vote_content2=0,avatar=''){
        let that=this;
        if(!title||!vote_content2)return;
        var nick_name=wx.getStorageSync('nickName');
   
        var uid=wx.getStorageSync('uid');
        var group_count=Number(vote_content1)+Number(vote_content2)
        let param2={
            title:title
        }
        wx.showLoading({
            title: '投票中...',
          })
          let param={
            uid:uid,
            title:title,
            content1:content1,
            content2:content2,
            group_count:group_count,
            vote_content1:vote_content1,
            vote_content2:vote_content2,
            idlist:[],
            nick_name:nick_name,
            avatar:avatar,
            
          }
        wx.Als.bmobFunc('vote_list','all',param2).then(res=>{
            if(res.length>0){
                return;
            }else{
                wx.Als.bmobFunc('vote_list', 'add', param).then(res => {
                    console.log(res)
                    console.log(res)
                    wx.hideLoading();
                    if(res){
                    
                      wx.showToast({
                        title: '添加成功~',
                      })
              
                    }
                  })
            }
        })
    
      
      
     },
    onVote: function(t) {
       
    },
    goHome(){
        wx.redirectTo({
          url: 'pages/index/index',
        })
    },
    onShareAppMessage: function(t) {
        var e = wx.getStorageSync("nickName"), a = t.target.dataset.voteid, o = e || "朋友";
         var uid=wx.getStorageSync("uid");
        if("button" === t.from){
        return "button" === t.from && console.log(t), {
            title: o + "邀你投一票！",
            path: "/pages/vote_detail/vote_detail?shareFrom=share&uid=" + uid + "&tid=" + a,
            success: function(t) {}
        };
      }else{
        return {
          title: o + "邀你投票！为赞同的意见投票,热门话题讨论",
              path: "/pages/index/index",
              success: function(t) {}
          };
      }
    },
    goHome(){
        wx.switchTab({
          url: '/pages/index/index',
        })
    },
    getVotes: function(e) {
        let that=this
        let param = {
            objectId: e,
           
          }
    
          var attr = {
            limit:1
          }
    
    
          wx.Als.bmobFunc('vote_list', 'all', param, attr).then(ret => {
            console.log(ret);

          let res=ret[0]
          if(res&&ret[0]){
          
           that.setData({
               vote:res
           })
           
          }
           
    
    
          }).catch(err => {
            console.log(err)
          })
    },
    Percentage: function(t, e) {
        return 0 == e ? 0 : (Math.round(t / e * 1e4) / 100).toFixed(0);
    },
    clickCollect: function(e) {
      
    }
});